export default class UniStorage {

	constructor() {

	}

	static get(key : string) : any {
		// console.log('UniStorage get:',key);
		const res : any = uni.getStorageSync(key);
		if (res) {
			const dataExpiredTime : number = res['storageExpiredTime'];
			// 判断是否设置了缓存时间
			if (dataExpiredTime) {
				// 超过缓存时间
				if (new Date().getTime() > dataExpiredTime) {
					this.remove(key);
					return null
				}
				return res['data'];
			}

			return res;
		} else {
			return null
		}

	}

	static set(key : string, value : any, expired ?: number) : void {

		// console.log('UniStorage set',key,value);

		if (expired && expired > 0) {
			uni.setStorageSync(key, {
				data: value,
				storageExpiredTime: expired
			})
		}
		uni.setStorageSync(key, value)
	}

	static remove(key : string) : void {
		uni.removeStorageSync(key);
	}

	static clearAll() : void {
		uni.clearStorageSync()
	}
}